<?php

class InvoiceController extends ExtCRUDController
{

    public function actionGetTreeListing()
    {
        if ($_POST['node'] == 'root') // root node
        {

            $index = array();
            $customers = Customer::model()->findAll();
            foreach ($customers as $customer)
            {
                $i = strtoupper($customer->LastName[0]);
                if (!isset($index[$i]))
                    $index[$i] = $i;
            }

            sort($index);
            $index = implode('', $index);

            $nodes = array();
            for ($a = 0; $a != strlen($index); $a++)
            {
                $nodes[] = array(
                    'text' => $index[$a] . ' <span style="font-size:9px">(' . Customer::model()->count("LastName like '" . $index[$a] . "%'") . ')</span>',
                    'id' => $index[$a],
                    'iconCls' => 'IconDocumentInvoice'
                );
            }
            echo CJSON::encode($nodes);
        }
        else
        {
            $customers = Customer::model()->findAll("LastName like '" . $_POST['node'] . "%'");
            $nodes = array();
            foreach ($customers as $customer)
            {
                $nodes[] = array(
                    'text' => $customer->LastName,
                        'leaf' => 'true',
                    'iconCls' => 'IconUserRedFemale',
                    'CustomerId' => $customer->CustomerId,
                    'LastName' => $customer->LastName
                );
            }
            echo CJSON::encode($nodes);
        }
    }

    protected function handleReadAction()
    {
        if (isset($_POST['id']))
        {
            $total = 1;
            $data = Invoice::model()->findAll('CustomerId = :cid', array(':cid' => $_POST['id']));
        }
        else
        {
            $total = 0;
            $data = array();
        }
        return $this->ExportToJSONDataSource($data, $total);
    }

    protected function handleCreateAction($rows)
    {
        return false;
    }

    protected function handleDestroyAction($id)
    {
        return false;
    }

    protected function handleUpdateAction($rows)
    {
        return true;
    }

}

?>
